package epam.bookShelf.web;

import java.sql.SQLException;

import javax.servlet.*;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import epam.bookShelf.database.BookShelfDAO_H2;

public  class ServletListener implements ServletContextListener {    
    public static BookShelfDAO_H2 dao = null;
    
    Logger log = Logger.getLogger(ServletListener.class);
    
    public static BookShelfDAO_H2 getDAO() {
    	return dao;
    }
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        BasicConfigurator.configure();
    	
    	try {
            dao = new BookShelfDAO_H2();
            dao.start();
        } catch (SQLException e) {
            log.error("DAO creating error");
            e.printStackTrace();
        }
        
        log.info("listener init. DAO created");
    }
    
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    	try {
			dao.stop();
		} catch (SQLException e) {
			log.error("DAO stopping error");
			e.printStackTrace();
		}
		
		log.info("DAO stopped successfully");
    }
}
